using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using SMSManager.Commn.Interfaces;
using SMSManager.Commn.Interfaces.ViewModels;
using SMSManager.Data;
using SMSManager.WpfClient.Properties;

namespace SMSManager.WpfClient.ViewModels
{
	[Export(typeof (ISMSSendViewModel))]
	[PartCreationPolicy(CreationPolicy.NonShared)]
	public class SMSSendViewModel : NotifyPropertyChangedBase, ISMSSendViewModel
	{
		private readonly CompositionContainer _container;

		[ImportingConstructor]
		public SMSSendViewModel(CompositionContainer container)
		{
			ResultMessage = string.Empty;
			_container = container;

			SendSMSCommand = new DelegateCommand(SendSMS);
			LogoutCommand = new DelegateCommand(Logout);
			OkCommand = new DelegateCommand(ConfirmResultMessage);
		}

		private void ActivateLoginView()
		{
			if (!SMSService.IsLogin)
			{
				ActivateView(Resources.LoginViewName);
			}
		}

		private void ActivateView(string viewName)
		{
			var regionManager = _container.GetExportedValue<IRegionManager>();
			IRegion region = regionManager.Regions[Resources.SMSServiceRegionName];
			object view = region.GetView(viewName);
			region.Activate(view);
		}

		private void Clear()
		{
			Message = "";
			this.OnPropertyChanged(x => x.Message);
		}

		private void ConfirmResultMessage()
		{
			ActivateView(Resources.SMSSendViewName);
		}

		private void Logout()
		{
			SMSService.Logout();

			this.OnPropertyChanged(x => x.IsLogin);

			ActivateLoginView();
		}

		private void SendSMS()
		{
			int sentCount = SMSService.SendSMS(SMSInfo);
			SetResultMessage(sentCount);
			ActivateView(Resources.SMSResultViewName);
			if (sentCount < 0)
			{
				return;
			}
			Clear();
		}

		private void SetResultMessage(int sentCount)
		{
			ResultMessage = sentCount >= 0
			                	? string.Format(Resources.SMSResultMessage, MessageCount, sentCount)
			                	: string.Format(Resources.SMSCountLackResultMessage, MessageCount, sentCount);
			this.OnPropertyChanged(x => x.ResultMessage);
		}

		public ISMSService SMSService { private get; set; }

		public ISMSInfo SMSInfo { private get; set; }

		public string ResultMessage { get; private set; }

		public ICommand OkCommand { get; set; }

		public int CharPerMessage
		{
			get { return SMSInfo.CharPerMessage; }
		}

		public bool IsLogin
		{
			get { return SMSService.IsLogin; }
		}

		public ICommand LogoutCommand { get; private set; }

		public string Message
		{
			get { return SMSInfo.Message; }
			set
			{
				SMSInfo.Message = value;
				this.OnPropertyChanged(x => x.CharCount);
				this.OnPropertyChanged(x => x.CharPerMessage);
				this.OnPropertyChanged(x => x.MessageCount);
			}
		}

		public int CharCount
		{
			get { return SMSInfo.CharCount; }
		}

		public int MessageCount
		{
			get { return SMSInfo.MessageCount; }
		}

		public string Receiver
		{
			get { return SMSInfo.Receiver; }
			set { SMSInfo.Receiver = value; }
		}

		public string SMSServiceName
		{
			get { return SMSService.Name; }
		}

		public ICommand SendSMSCommand { get; private set; }

		public string Sender
		{
			get { return SMSInfo.Sender; }
			set { SMSInfo.Sender = value; }
		}

		public string this[string propertyName]
		{
			get { return SMSInfo[propertyName]; }
		}

		public string Error
		{
			get { throw new NotSupportedException(); }
		}
	}
}